package com.javaclimb.controller;

import com.alibaba.fastjson.JSONObject;
import com.javaclimb.pojo.Comment;
import com.javaclimb.pojo.User;
import com.javaclimb.service.CommentService;
import com.javaclimb.utils.Consts;
import com.javaclimb.utils.PageBean;
import com.javaclimb.utils.Result;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;



    @GetMapping("/getAllComment")
    public Object getAllComment(HttpServletRequest request) {
        String treeId = request.getParameter("treeId");
        return commentService.getComments(Integer.parseInt(treeId));
    }

    @PostMapping("postComment")
    public Object postComment(HttpServletRequest request) {
        String treeId= request.getParameter("treeId");
        String userId= request.getParameter("userId");
        String content = request.getParameter("content").trim();
        String username = request.getParameter("username").trim();
        String avator = request.getParameter("avator").trim();

        JSONObject jsonObject=new JSONObject();
        Comment comment=new Comment();
        comment.setTreeId(Integer.parseInt(treeId));
        comment.setUserId(Integer.parseInt(userId));
        comment.setContent(content);
        comment.setAvator(avator);
        comment.setUsername(username);

        boolean flag = commentService.addComment(comment);
        if(flag){
            jsonObject.put(Consts.CODE, 1);
            jsonObject.put(Consts.MSG,"评论成功");
            return jsonObject;
        }else {
            jsonObject.put(Consts.CODE, 0);
            jsonObject.put(Consts.MSG,"评论失败");
            return jsonObject;
        }
    }

    @PostMapping("/addLike")
    public Object addLike(HttpServletRequest request) {
        String id= request.getParameter("id");

        JSONObject jsonObject=new JSONObject();

        boolean flag = commentService.addLike(Integer.parseInt(id));
        if(flag){
            jsonObject.put(Consts.CODE, 1);
            return jsonObject;
        }else {
            jsonObject.put(Consts.CODE, 0);
            return jsonObject;
        }
    }

    /**
     * 根据userid删除评论
     */
    @PostMapping("deleteComment")
    public Object deleteComment(HttpServletRequest request) {
        JSONObject jsonObject=new JSONObject();
        String id= request.getParameter("id").trim();
        boolean flag = commentService.deleteComment(Integer.parseInt(id));
        if(flag){
            jsonObject.put(Consts.CODE, 1);
            return jsonObject;
        }else {
            jsonObject.put(Consts.CODE, 0);
            return jsonObject;
        }
    }

    /**
     * 根据userid和treeId检查时候已经评论过了
     */
    @GetMapping("/checkIsComment")
    public Object checkIsComment(HttpServletRequest request) {
        JSONObject jsonObject=new JSONObject();
        String userId= request.getParameter("userId");
        String treeId= request.getParameter("treeId");

        Comment comment = commentService.checkIsComment(Integer.parseInt(userId),Integer.parseInt(treeId));
        if(comment!=null){
            jsonObject.put(Consts.CODE, 1);
            return jsonObject;
        }else {
            jsonObject.put(Consts.CODE, 0);
            return jsonObject;
        }
    }

    /////////////////////////////后台//////////////////////////////////////////
    @PostMapping(value = "/selectComment")
    public Result page(@RequestBody Comment comment){
        log.info("分页查询，参数：{}，{}，{}，{}，{},{},{}",comment.getPage(),comment.getPageSize(),comment.getId(),comment.getUserId(),comment.getTreeId(),comment.getStartTime(),comment.getEndTime());
        //调用service分页查序
        PageBean pB = commentService.page(comment.getPage(),comment.getPageSize(),comment.getId(),comment.getUserId(),comment.getTreeId(),comment.getStartTime(),comment.getEndTime());
        return Result.success(pB);
    }

    /**
     * 删除评论
     */
    @DeleteMapping("/{ids}")//传递地址参数
    public Result delete(@PathVariable List<Integer> ids){
        log.info("批量删除操作，ids:{}",ids);
        commentService.delete(ids);
        return Result.success();
    }

}
